local skel = fk.CreateSkill {
  name = "rmt__zeji",
}

---@param player ServerPlayer
---@return any
--检索所有被此技能拥有者弃置过牌的角色和拥有过的牌
local searchZeji = function (player)
  local ids = {}
  player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function (e)
    for _, move in ipairs(e.data) do
      if move.from and table.contains(player.room.alive_players, move.from) and move.proposer == player and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            table.insertIfNeed(ids, move.from)
          end
        end
      end
      if move.to == player and move.toArea == Player.Hand then
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          player.room:setCardMark(card, "rmt__zeji"..player.id, 1)
        end
      end
    end
  end, Player.HistoryGame)
  for _, p in ipairs(ids) do
    player.room:setPlayerMark(p, "rmt__zeji_tar", 1)
  end
end

skel:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  prompt = "#rmt__zeji",
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryPhase)
  end,
  target_tip = function (self, player, to, selected)
    if to:getMark("rmt__zeji_tar") > 0 and not table.find(to:getCardIds("he"), function (id) return Fk:getCardById(id):getMark("rmt__zeji"..player.id) > 0 end) then
      return "@@rmt__zeji_tar"
    elseif table.find(to:getCardIds("he"), function (id) return Fk:getCardById(id):getMark("rmt__zeji"..player.id) > 0 end) and to:getMark("rmt__zeji_tar") == 0 then
      return "@@rmt__zeji_max"
    elseif table.find(to:getCardIds("he"), function (id) return Fk:getCardById(id):getMark("rmt__zeji"..player.id) > 0 end) and to:getMark("rmt__zeji_tar")  > 0 then
      return "@@rmt__zeji_all"
    end
  end,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to, selected)
    return #selected == 0 and (to:getMark("rmt__zeji_tar") > 0 or
    table.find(to:getCardIds("he"), function (id) return Fk:getCardById(id):getMark("rmt__zeji"..player.id) > 0 end))
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    local choices = {"rmt__zeji_damage", "rmt__zeji_recover", "rmt__zeji_all"}
    if to:getMark("rmt__zeji_tar") == 0 or not table.find(to:getCardIds("he"), function (id) return Fk:getCardById(id):getMark("rmt__zeji"..player.id) > 0 end) then
      choices = {"rmt__zeji_damage"}
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = skel.name,
      prompt = "#rmt__zeji-back",
    })
    while not player.dead do
      if choice ~= "rmt__zeji_recover" then
        room:damage { from = player, to = to, damage = 1, skillName = skel.name }
      end
      if choice ~= "rmt__zeji_damage" then
        if not to:isKongcheng() then
          room:askToChooseCards(player, {
            target = to,
            min = 0,
            max = 0,
            flag = { card_data = {{ to.general, to:getCardIds("h") }} },
            skill_name = skel.name,
            prompt = "#rmt__zeji-watch",
          })
        end
        room:recover{ who = player, recoverBy = player,  num= 1, skillName = skel.name }
      end
      if choice == "rmt__zeji_all" then
        room:loseHp(player, 1, skel.name)
      end
      break
    end
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if not player:hasSkill(skel.name, true) then return false end
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Player.Hand then
        return true
      end
      if move.proposer == player and move.moveReason == fk.ReasonDiscard then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            return true
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    searchZeji(player)
  end,
})

skel:addAcquireEffect(function (self, player, is_start)
  searchZeji(player)
end)

Fk:loadTranslationTable{
  ["rmt__zeji"] = "择机",
  [":rmt__zeji"] = "出牌阶段限一次，你可选择一名拥有你曾拥有过的牌或你弃置过其牌的角色，对其造成1点伤害。乘势：你观看其手牌并回复1点体力。背水：失去1点体力。",

  ["#rmt__zeji"] = "你可以对拥有曾被你拥有的牌的角色，或被你弃置过牌的角色：造成1点伤害；或观看其手牌并回复1点体力；或失去1点体力。",
  ["rmt__zeji_damage"] = "对其造成1点伤害",
  ["rmt__zeji_recover"] = "观看其手牌并回复1点体力",
  ["rmt__zeji_all"] = "背水：失去1点体力",
  ["@@rmt__zeji_tar"] = "被你弃置过牌",
  ["@@rmt__zeji_max"] = "拥有你拥有过的牌",
  ["@@rmt__zeji_all"] = "均满足",
  ["#rmt__zeji-back"] = "择机：请选择一项！",
  ["#rmt__zeji-watch"] = "择机：正在观看手牌，可以点击确定提前结束",

  ["$rmt__zeji1"] = "大丈夫当行侠重义，仗剑天下。",
  ["$rmt__zeji2"] = "路见不平，拔刀相助，乃侠者之义也。",
}

return skel
